package com.bright.cwduijie.duijie.web.controller;

import com.bright.cwduijie.common.util.RestResult;
import com.bright.cwduijie.duijie.pojo.query.DuijieBaseDataQuery;
import com.bright.cwduijie.duijie.pojo.query.DuijieStatisticsQuery;
import com.bright.cwduijie.duijie.pojo.query.TreeStatisticsQuery;
import com.bright.cwduijie.duijie.service.StatisticsService;
import com.bright.cwduijie.duijie.service.WebExchangeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * <p> Project: cwduijie - QueryDuijieController </p>
 *
 * @author Tz
 * @version 1.0.0
 * @date 2024/08/01 9:50
 * @since 1.0.0
 */
@RequestMapping("/query")
@Controller
@RequiredArgsConstructor
@RestController
@Slf4j
public class QueryDuijieController {

    private final StatisticsService statisticsService;

    @Qualifier("normal")
    private final WebExchangeService webExchangeService;


    @RequestMapping("/getBaseDuijieData")
    public RestResult getBaseDuijieData(DuijieBaseDataQuery duijieQuery) {
        return RestResult.success(statisticsService.getBaseDuijieData(duijieQuery));
    }


    @RequestMapping("/getStatisticsMonthsData")
    public RestResult getStatisticsMonthsData(DuijieStatisticsQuery duijieQuery) {
        return RestResult.success(statisticsService.getStatisticsMonthsData(duijieQuery));
    }

    @RequestMapping("/getStatisticsData")
    public RestResult getStatisticsData(DuijieStatisticsQuery duijieQuery) {
        return RestResult.success(statisticsService.getStatisticsData(duijieQuery));
    }

    @RequestMapping("/getFailDetailData")
    public RestResult getFailDetailData(DuijieBaseDataQuery duijieQuery) {
        return RestResult.success(statisticsService.getFailDetailData(duijieQuery));
    }

    @RequestMapping("/getDuijieAsyncData")
    public RestResult getDuijieAsyncData(DuijieBaseDataQuery duijieQuery) {
        return RestResult.success(statisticsService.getDuijieAsyncData(duijieQuery));
    }

    @RequestMapping("/getExecutingDuijieBatchData")
    public RestResult getExecutingDuijieBatchData(DuijieBaseDataQuery duijieQuery) {
        return RestResult.success(statisticsService.getExecutingDuijieBatchData(duijieQuery));
    }

    /**
     * 获取按地区汇总的统计数据 仿照信巴迪平台
     * @param duijieQuery
     * @return
     */
    @RequestMapping("/getTreeStatisticsData")
    public RestResult getTreeStatisticsData(@Valid TreeStatisticsQuery duijieQuery) {
        return RestResult.success(statisticsService.getTreeStatisticsData(duijieQuery));
    }

    /**
     * 获取地区树
     */
    @RequestMapping("/dist")
    public RestResult getDist(@RequestParam String distNo, @RequestParam @NotNull Integer level) {
        return RestResult.success(statisticsService.getDist(distNo, level));
    }

    /**
     * 导出
     * @param duijieQuery
     * @return
     */
    @RequestMapping("/exportTreeStatisticsData")
    public RestResult exportTreeStatisticsData(@Valid TreeStatisticsQuery duijieQuery, HttpServletResponse response) {
        try {
            statisticsService.exportExcelOfTreeStatisticsData(duijieQuery, response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

}
